package com.dpiinc.TechPage.widget;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.TextView;

import com.dpiinc.TechPage.R;

/**
 * Created by hu489_000 on 2018-07-07.
 */

public class LoadingView extends ProgressDialog {

    private static LoadingView loading;

    TextView tv_load_dialog;
    private LoadingView(Context context) {
        super(context);
    }
    private LoadingView(Context context, int theme) {
        super(context, theme);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init(getContext());
    }
    private void init(Context context) {
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        setContentView(R.layout.loading_view);//loading的xml文件
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(params);
        tv_load_dialog = findViewById(R.id.tv_load_dialog);
    }
    @Override
    public void show() {//开启
        super.show();
    }
    @Override
    public void dismiss() {//关闭
        super.dismiss();
    }

    public void setText(String str){
        tv_load_dialog.setText(str);
    }

    public static void createLoadingViewAndShow(final Context context, int customDialog, final String show) {
        ((Activity)context).runOnUiThread(new Runnable() {
           public void run() {
                loading = new LoadingView(context,R.style.CustomDialog);
                loading.show();
                loading.setText(show);
            }
        });
    }

    public static void dismissLoadingView(){
        if(loading != null && loading.isShowing()){
            loading.dismiss();
        }
    }
}